
let express = require('express');
let router = express.Router();
let filter = require('../../components/filter');
    
/**
 * 查询分类数据 2018-10-15
 *
 * 按条件查询分类数据
 *
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    req.app.db.query("select count(1) as totalCount from classify " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from classify " + sql + " order by code " + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }
            
            res.json({"success": true, "data": results, "totalCount": totalCount, "message": ""});
        });
    });
});

/**
 * 查看分类数据 2018-10-15
 *
 * 按id查看指定的分类信息
 *
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from classify where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改分类数据 2018-10-15
 *
 *
 */
router.put('/:id', filter.authorize, function (req, res, next) {
    let params = req.body;
    let user = req.session.user;
    let now = new Date();

    
    if (params.id === undefined || params.id === '') {
        res.json({"success": false, "message": "分类编码错误！"});
        return;
    }
    
    if (params.name === undefined || params.name === '' ) {
        res.json({"success": false, "message": "分类名称不能为空！"});
        return;
    }
    
    req.app.db.query("update classify set name = ?, userId = ?, userName = ?, updateDate = ? where id = ?", [params.name, user.id, user.trueName, now, params.id], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });
});

/**
 * 新增分类数据 2018-10-15
 *
 *
 */
router.post('/', filter.authorize, function (req, res, next) {
    let params = req.body;
    let user = req.session.user;
    let code;
    let now = new Date();
        
    if (params.code === undefined || params.code === '' ) {
        res.json({"success": false, "message": "分类代码不能为空！"});
        return;
    }

    code = params.code.toUpperCase();
    if (code.length > 10) {
        res.json({"success": false, "message": "分类代码长度不能大于10位！"});
        return;
    }

    if (params.name === undefined || params.name === '' ) {
        res.json({"success": false, "message": "分类名称不能为空！"});
        return;
    }

    req.app.db.query("select * from classify where code = ?", [code], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length > 0) {
            res.json({"success": false, "message": "分类代码重复！"});
            return;
        }

        req.app.db.query("insert into classify(code, name, userId, userName, createDate, updateDate) values(?, ?, ?, ?, ?, ?)", [code, params.name, user.id, user.trueName, now, now], function (err, results) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }

            res.json({"success": true, "message": global.postMsg});
        });
    });
});
    
/**
 * 删除分类信息 2018-10-15
 *
 * 按id删除指定的分类信息
 */
router.delete('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query('delete from classify where id = ?', [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.deleteMsg});
    });
});
    
module.exports = router;
    